<?php
 strtolower(basename($_SERVER['PHP_SELF'])) == 'User.class.php' && header('Location:http://' . $_SERVER['HTTP_HOST']); //禁止直接访问本页

    include('db.php');
    class User{
    	private $id;
    	private $name;
    	private $pwd;
    	public function __construct($id='',$name = '',$pwd=''){
    		$this->id=$id;
    		$this->name=$name;
    		$this->pwd=$pwd;
    	}
		
		public function __get($attrName){
			return $this->$attrName;
		}
    	
		public function __set($attrname,$attrvalue){
			$this->$attrname=$attrvalue;
		}
		
		public function __call($method,$p){
			if($method=='showMsg'){
				echo $this->__get($p[0]);
			}
		}
		
    	public static function getUserByName($name=''){
    		if (empty($name))
    			return null;
    		
    		$sql="select * from user where name='{$name}'";
    		$mydb=new MySql();
    		$resultArray=$mydb->query($sql);
    		$res=mysql_fetch_array($resultArray);
		    if($res){
		    	$user=new User($res['id'],$res['name'],$res['pwd']);
		    	return $user;
		    }
		    return null;
    	}
		
		public static function getUserById($id=''){
    		if (empty($id))
    			return null;
    		
    		$sql="select * from user where id='{$id}'";
    		$mydb=new MySql();
    		$resultArray=$mydb->query($sql);
    		$res=mysql_fetch_array($resultArray);
		    if($res){
		    	$user=new User($res['id'],$res['name'],$res['pwd']);
		    	return $user;
		    }
		    return null;
    	}
    	
    	public static function getUserByNameAndPwd($name='',$pwd=''){
    		if (empty($name)||empty($pwd))
    			return null;
    		
    		$sql="select * from user where name='{$name}' and pwd='${pwd}'";
    		$mydb=new MySql();
    		$resultArray=$mydb->query($sql);
    		$res=mysql_fetch_array($resultArray);
		    if($res){
		    	$user=new User($res['id'],$res['name'],$res['pwd']);
		    	return $user;
		    }
		    return null;
    	}
    	
    	public static function addUser($name='',$pwd=''){
    		if (empty($name)||empty($pwd))
    		   return false;
    		$user=User::getUserByName($name);
    		if($user)
    		     return false;
    		$insertSql="INSERT INTO `student`.`user` (`id`, `name`, `pwd`) VALUES (NULL, '$name', '$pwd')";
	        $mydb=new MySql();
    		$res=$mydb->query($insertSql);
    		if($res){
    			return true;
    		}else{
    			return false;
    		}
    	}
    	
    	public static function getUserList(){
    		$queryAll="select * from user order by name";
    		$mydb=new MySql();
    		$resultArray=$mydb->query($queryAll);
    		$arr=array();
    		$idx=0;
    		while($row=$mydb->fetch($resultArray)){
    			$user=new User($row['id'],$row['name'],$row['pwd']);
    			$arr[$idx]=$user;
    			$idx++;
    		}
    		return $arr;
    		
    	}
    	
    	public function getName(){
    		return $this->name;
    	}
    	
    	public function getPwd(){
    		return $this->pwd;
    	}
    	
    	public function getId(){
    		return $this->id;
    	}
		
		public static function delUserById($id=''){
			$delSql="delete from user where id={$id}";
			$mydb=new MySql();
			$exe=$mydb->query($delSql);
			if($exe)
			   return true;
            else
				return false;
		} 
		
		public static function updateUser($id='',$name='',$pwd=''){
			$upsql="UPDATE  `student`.`user` SET  `name` =  '$name',`pwd` =  '$pwd' WHERE  `user`.`id` ={$id}";
			$mydb=new MySql();
			$exe=$mydb->query($upsql);
			if($exe)
			   return true;
            else
				return false;
		} 
    	
    }
?>
